package com.yunji.aggregate.mapper;

import com.yunji.aggregate.model.dto.analysis.LinkageQueryParam;
import com.yunji.aggregate.model.dto.analysis.ProductOrderStatisticsParam;
import com.yunji.aggregate.model.dto.order.StatsOrdersParams;
import com.yunji.aggregate.model.dto.product.PricingParam;
import com.yunji.aggregate.model.dto.product.ProductDTO;
import com.yunji.aggregate.model.vo.analysis.LinkageQueryVO;
import com.yunji.aggregate.model.vo.order.StatsOrdersVO;
import com.yunji.aggregate.model.vo.product.PricingProductStatisticsVO;
import com.yunji.aggregate.model.vo.product.ProductOrderStatisticsVO;
import com.yunji.aggregate.model.vo.product.ProductStatisticsVO;
import com.yunji.common.annotation.DataScope;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface IProductAggregateMapper {
    List<ProductStatisticsVO> searchProductList(ProductDTO dto);

    List<PricingProductStatisticsVO> searchPricingList(PricingParam param);

    List<String> searchBarAgentName(String agentName);

    List<String> searchBarManagerName(PricingParam param);

    String PRODUCT_ORDER_WHERE_SQL = "<if test='startTime != null'> and h.create_time &gt;= #{startTime}</if>" +
            "<if test='endTime != null'> and h.create_time &lt;= #{endTime}</if>";

    @DataScope(children = true, deep = 1, scopeTables = {"product_agent", "t_order"})
    @Select({"<script>select p.id product_id,p.sku product_sku,p.product_name, p.album_pics, p.product_type,order_count,order_avg_amt,order_sum_amt,order_sum_cost,",
            " r.refund_count / o.order_count * 100 refund_rate,order_sum_amt-order_sum_cost gross_profit,(order_sum_amt-order_sum_cost)/order_sum_amt*100 gross_rate",
            " from product p left join (select d.product_id,count(d.order_detail_id) order_count,avg(d.payable_amount) order_avg_amt,sum(d.payable_amount) order_sum_amt,",
            " sum(d.cost_price) order_sum_cost from t_order_detail d,t_order h where h.order_status=2 and h.order_id=d.order_id" + PRODUCT_ORDER_WHERE_SQL,
            " group by d.product_id) o on o.product_id=p.id ",
            " left join (select product_id,count(*) refund_count from t_refund_detail d,t_refund h where h.refund_id=d.refund_id and h.apply_status=2" + PRODUCT_ORDER_WHERE_SQL,
            " group by d.product_id ) r on r.product_id=p.id ",
            // 这个inner join 是为了拼接权限相关数据 因为商品无belongId
            "inner join (select product_id from product_agent pa where pa.del_flag=0 and pa.status=0 group by product_id)pa on p.id=pa.product_id",
            " where p.del_flag=0 <if test='productType!=null'> and p.product_type=#{productType}</if>",
            " <if test='productIdList!=null and productIdList.size()>0'> and p.id in ",
            " <foreach collection='productIdList' item='pid' open='(' separator=',' close=')'>#{pid}</foreach></if>",
            " ${orderSql}</script>"})
    List<ProductOrderStatisticsVO> productSalesStats(ProductOrderStatisticsParam param);

    @DataScope(children = true, deep = 1, scopeTables = {"product_agent"})
    List<LinkageQueryVO> productLinkageQuery(LinkageQueryParam param);

    List<StatsOrdersVO> statsOfOrdersByProduct(StatsOrdersParams params);
}
